//
//  ICPDFReadController.m
//  ICrEAM
//
//  Created by mac on 16/9/21.
//  Copyright © 2016年 Yonyou. All rights reserved.
//

#import "ICPDFReadController.H"
#import <WebKit/WebKit.h>

@interface ICPDFReadController()<WKNavigationDelegate,WKUIDelegate>
@property(nonatomic,strong)WKWebView *webView;
@property(nonatomic,strong)UIWebView *fileWebView;
@end
@implementation ICPDFReadController

-(void)viewDidLoad{
    [super viewDidLoad];
     NSURL *targetURL = [NSURL fileURLWithPath:self.filePath];
    if (( [[[UIDevice currentDevice] systemVersion] compare:@"9.0"] != NSOrderedAscending )) {
        [self.view addSubview:self.webView];
         [self.webView loadFileURL:targetURL allowingReadAccessToURL:targetURL];
    }else{
        [self.view addSubview:self.fileWebView ];
        switch (self.documentType) {
            case TxtType:{
                        NSData *Data = [NSData dataWithContentsOfFile:self.filePath];
                     [self.fileWebView loadData:Data MIMEType:@"text/plain" textEncodingName:@"GBK" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
            }break;
//                case PdfType:
//                 [self.fileWebView loadData:Data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:[NSURL fileURLWithPath:NSTemporaryDirectory()]];
//                break;

                
            default:{
                 NSMutableURLRequest *re=[NSMutableURLRequest requestWithURL:targetURL];
                [self.fileWebView loadRequest:re];
            
            } break;
        }
    }
   
    
}
-(UIWebView *)fileWebView{
    if (!_fileWebView) {
        _fileWebView=[[UIWebView alloc]initWithFrame:self.view.bounds];
        _fileWebView.delegate=self;
        [_fileWebView sizeToFit];
        _fileWebView.scalesPageToFit = YES;
         _fileWebView.multipleTouchEnabled = YES;
    }
    return _fileWebView;
}
-(WKWebView *)webView{
    if (!_webView) {
        _webView=[[WKWebView alloc]initWithFrame:self.view.bounds];
        _webView.backgroundColor=[UIColor clearColor];
        [_webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
        [_webView setNavigationDelegate:self];
        [_webView setUIDelegate:self];
        [_webView setMultipleTouchEnabled:YES];
        [_webView setAutoresizesSubviews:YES];
        [_webView.scrollView setAlwaysBounceVertical:YES];
        [_webView setAllowsBackForwardNavigationGestures:true];
    }
    return _webView;
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
   
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
    
    decisionHandler(WKNavigationResponsePolicyAllow);
}

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    
}
#pragma mark UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
@end
